package domainLogic;

import java.io.IOException;
import java.sql.SQLException;
import java.sql.Timestamp;

/**
 * @author  Tim Moon
 */
public class Item {

	private int id;
	private String user_id;
	private int cat_id;
	private String name;
	private String description;
	private int startingprice;
	private int reserveprice;
	private int buyoutprice;
	private Timestamp enddate;
	private Timestamp realenddate;
	private char endnotified;
	private Timestamp regdate;
	private String filename;
	
	public Item(String user_id, int cat_id, String name, String description, int startingprice,
				int reserveprice, int buyoutprice, Timestamp enddate, Timestamp realenddate, 
				String filename) {
		this.user_id = user_id;
		this.cat_id = cat_id;
		this.name = name;
		this.description = description;
		this.startingprice = startingprice;
		this.reserveprice = reserveprice;
		this.buyoutprice = buyoutprice;
		this.enddate = enddate;
		this.realenddate = realenddate;
		this.filename = filename;		
	}
	
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUserId() {
		return user_id;
	}
	public void setUserId(String user_id) {
		this.user_id = user_id;
	}
	public int getCatId() {
		return cat_id;
	}
	public void setCatId(int cat_id) {
		this.cat_id = cat_id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public int getStartingprice() {
		return startingprice;
	}
	public void setStartingprice(int startingprice) {
		this.startingprice = startingprice;
	}
	public int getBuyoutprice() {
		return buyoutprice;
	}
	public void setBuyoutprice(int buyoutprice) {
		this.buyoutprice = buyoutprice;
	}
	public int getReserveprice() {
		return reserveprice;
	}
	public void setReserveprice(int reserveprice) {
		this.reserveprice = reserveprice;
	}
	public Timestamp getEnddate() {
		return enddate;
	}
	public void setEnddate(Timestamp enddate) {
		this.enddate = enddate;
	}
	public char getEndnotified() {
		return endnotified;
	}
	public void setEndnotified(char endnotified) {
		this.endnotified = endnotified;
	}
	public Timestamp getRealenddate() {
		return realenddate;
	}
	public void setRealenddate(Timestamp realenddate) {
		this.realenddate = realenddate;
	}
	public Timestamp getRegdate() {
		return regdate;
	}
	public void setRegdate(Timestamp regdate) {
		this.regdate = regdate;
	}
	
	public String getFilename() {
		return filename;
	}
	public void setFilename(String filename) {
		this.filename = filename;
	}
	
//	public Item()  { }
	public Item(java.sql.ResultSet rs)
	{
		try {
			setId(rs.getInt("id"));
			setUserId(rs.getString("user_id"));
			setCatId(rs.getInt("cat_id"));
			setName(rs.getString("name"));
			setDescription(rs.getString("description"));
			setStartingprice(rs.getInt("startingprice"));
			setReserveprice(rs.getInt("reserveprice"));
			setBuyoutprice(rs.getInt("buyoutprice"));
			setEnddate(rs.getTimestamp("enddate"));
			setRealenddate(rs.getTimestamp("realenddate"));
			setEndnotified(rs.getString("endnotified").charAt(0));
			setRegdate(rs.getTimestamp("regdate"));
			setFilename(rs.getString("filename"));			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public boolean validate(javax.servlet.jsp.JspWriter out) throws IOException
	{
		// Chceck price
		if(this.getStartingprice() > this.getReserveprice() && this.getReserveprice() > 0)
		{
			out.println(common.JspUtil.makeErrorMsg("Reserve price should be more than starting price!"));
			return false;
		}
		if(this.getStartingprice() > this.getBuyoutprice() && this.getBuyoutprice() > 0)
		{
			out.println(common.JspUtil.makeErrorMsg("Buyout price should be more than starting price!"));
			return false;
		}
		if(this.getReserveprice() > this.getBuyoutprice() && this.getBuyoutprice() > 0 && this.getReserveprice() > 0)
		{
			out.println(common.JspUtil.makeErrorMsg("Buyout price should be more than Reserve price!"));
			return false;
		}		
		
		// Check end date is after 10 minutes
		java.util.Date availabeEnddate = new java.util.Date(java.util.Calendar.getInstance().getTimeInMillis() + 10*60*1000);
		if(this.getEnddate().before(availabeEnddate))
		{
			out.println(common.JspUtil.makeErrorMsg("The end date and time should be after 10 minutes from now : "+ availabeEnddate));
			return false;
		}
		return true;
		
	}

}

